package checkers3d.logic;

import checkers3d.presentation.IInputObserver;
import java.awt.Point;
import java.awt.Rectangle;

/**
 *
 * @author Reda Herbzi
 * @author Ruben Acuna
 */
public class PlayerControllerLocal implements IPlayerController, IInputObserver
{
    Player player;
    int tileSize;
    Point3D lastClicked = null;
    Rectangle[] boardBoundingBoxes;

    public PlayerControllerLocal(int boardSizeX, int boardSizeY,
                                 Point[] boardLocations,
                                 int tileSize, Player player)
    {
        this.tileSize = tileSize;
        setPlayer(player);

        boardBoundingBoxes = new Rectangle[boardLocations.length];

        for(int board = 0; board < boardBoundingBoxes.length; board++)
            boardBoundingBoxes[board] = new Rectangle(boardLocations[board].x,
                                                      boardLocations[board].y,
                                                      boardSizeX * tileSize,
                                                      boardSizeX * tileSize);
    }

    public void onClick(Point position)
    {
        int x = position.x;
        int y = position.y;

        lastClicked = null;

        // if the x coordinate is on the left side of the screen, then the point
        // was clicked on the first board, otherwise it was on the second board
        for(int board = 0; board < boardBoundingBoxes.length; board++)
        {
            if(boardBoundingBoxes[board].contains(x, y))
            {
                int pointX = (x - boardBoundingBoxes[board].x) / tileSize;
                int pointY = (y - boardBoundingBoxes[board].y) / tileSize;
                int pointZ = board;

                lastClicked = new Point3D(pointX, pointY, pointZ);
            }
        }
    }

    public void onKeyPress(char key)
    {
        //do nothing.
    }

    /* x and y being the coordinates of the point clicked BoardX and BoardY
       represents the limits of a square. */
    public Point3D popLastMove()
    {
        Point3D last = lastClicked;

        lastClicked = null;

        return last;
    }

    public void setPlayer(Player player)
    {
        this.player = player;
    }

    public Player getPlayer()
    {
        return player;
    }
}
 